 
package planclient;



import java.util.List;

import modele.Employe;

import org.eclipse.e4.core.contexts.EclipseContextFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.basic.MBasicFactory;
import org.eclipse.e4.ui.model.application.ui.basic.MInputPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
import org.eclipse.ui.IViewPart;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;

public class AddEmployeSched{
	private MInputPart inputPart;
	@Execute
	public void execute() {
		//TODO Your code goes here
		log.logger.Infologg("[EXEC TEST A]");
	}
	@Execute
	public void execute(EPartService partService2, MApplication application2,
	        EModelService modelService2, Employe employe) {
		
		Bundle bundle = FrameworkUtil.getBundle(EPartService.class);

		BundleContext bundleContext = bundle.getBundleContext();

		IEclipseContext eclipseCtx =
				EclipseContextFactory.getServiceContext(bundleContext);
		MApplication application = eclipseCtx.get(MApplication.class);
		
		EPartService  partService  = (EPartService) eclipseCtx.get(EPartService.class.getName());
        EModelService modelService = (EModelService) application.getContext()
                .get(EModelService.class.getName());

		
		log.logger.Infologg("[EXEC TEST B]");
		

		MPart part = MBasicFactory.INSTANCE.createPart();
	    part.setLabel(employe.toString());
	    
	    part.setCloseable(true);
	    //part.setContributionURI("bundleclass://plannin-tools/planclient.AffEmployeSched");
	    part.setContributionURI("bundleclass://plannin-tools/planclient.AffEmployeAgenda");

	     List<MPartStack> stacks = modelService.findElements(application, null, MPartStack.class, null);
	    //plannin-tools.partstack.0
	    for (int i =0 ; i < stacks.size() ; i ++ ){
	    	if (stacks.get(i).getElementId().equalsIgnoreCase("plannin-tools.partstack.0")){
	   	     	stacks.get(i).getChildren().add(part);

	    	}
	    			
	    }
	    	
	     part.setObject(employe);

	   partService.showPart(part, PartState.ACTIVATE);
	   
	

	     

	}
		
}